package com.example.ch3_2thread;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputContentInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private Button btnBaidu, btnImg;
    private TextView tvBaidu;
    private ImageView ivImg;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件对象
        getViews();
        //注册事件监听器
        setListener();

        //初始化handler
        handler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                //判断msg
                switch (msg.what){
                    case 1:
                        //获取封装的数据
                        Bitmap bitmap = (Bitmap) msg.obj;
                        //将获取到的数据显示到图像控件中
                        ivImg.setImageBitmap(bitmap);
                        break;

                }
            }
        };

    }

    private void setListener() {
        //实例化自定义的点击事件监听器
        OnCustomClickListener listener = new OnCustomClickListener();
        btnBaidu.setOnClickListener(listener);
        btnImg.setOnClickListener(listener);



    }

    private void getViews() {
        btnBaidu = findViewById(R.id.btn_baidu);
        tvBaidu = findViewById(R.id.tv_baidu);

        btnImg = findViewById(R.id.btn_img);
        ivImg = findViewById(R.id.iv_img);

    }


    class OnCustomClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_baidu:
                    //访问百度
                    visitBaidu();
                    break;
                case R.id.btn_img:
                    //下载图片,并在图像控件中显示
                    downloadImg("https://img.wxcha.com/m00/90/a5/d1d167451213c94da52f7ddf31d8da3b.jpg");
                    break;
            }
        }
    }

    private void downloadImg(String imgUrl) {
        //启动线程下载图
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    //1.实例化URL对象
                    URL url = new URL(imgUrl);
                    //2.获取图片对应的网络输入流
                    InputStream in = url.openStream();
                    //3.利用Bitmap工厂将图片对应的输入流转成Bitmap对象
                    Bitmap bitmap = BitmapFactory.decodeStream(in);
                    //4.获取Message对象，并封装数据
                    Message msg = handler.obtainMessage();
                    //封装bitmap
                    msg.obj = bitmap;
                    //标记一下当前的Message
                    msg.what = 1;
                    //5.通过handle引用将Message对象发布出去
                    handler.sendMessage(msg);


                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    private void visitBaidu() {
        //访问百度
        //启动子线程
        new Thread(){
            @Override
            public void run() {

                //1.实例化URL对象
                try {
                    URL url  = new URL("Https://www.baidu.com");

                    //2.获取网络输入流
                    InputStream in = url.openStream();
                    //3.利用循环读取输入流中的数据
                    //定义循环变量
                    byte[] bytes = new byte[256];
                    //定义一个字符串变量类的对象，用雨存储读到的数据
                    StringBuffer buffer = new StringBuffer();
                    while ((in.read(bytes))!=-1){
                        //把字节数组转成字符串
                        String str = new String(bytes,0,bytes.length);
                        buffer.append(str);
                    }
                    //用日志显示读到的数据
                    Log.i("msg",buffer.toString());
                    //在主线程中更新UI界面
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvBaidu.setText(buffer.toString());
                        }
                    });

                    //4.关闭流
                    in.close();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }.start();



    }


}